<template>
	<view style="padding: 10px 20px;">
		<view class="ui-all" style="display: flex;flex-direction: column;gap: 10px;">
			<view class="ui-list" style="display: flex;align-items: center;gap: 10px;">
				<text>产品标题</text>
				<uni-easyinput v-model="depict.title" trim="all"></uni-easyinput>
			</view>
			<view class="ui-list" style="display: flex;align-items: center;gap: 10px;">
				<text>副副标题</text>
				<uni-easyinput v-model="depict.jingle" trim="all"></uni-easyinput>
			</view>
			<view class="ui-list" style="display: flex;align-items: center;gap: 10px;">
				<text>详细介绍</text>
				<uni-easyinput v-model="depict.content" autoHeight type="textarea" trim="all"></uni-easyinput>
			</view>
			<view class="ui-list" style="display: flex;align-items: center;gap: 10px;justify-content: flex-end;">
				<view class="gl-button gl-button-base" @tap="EventSubmit">
					<uni-icons type="checkmarkempty" size="18" color="#fff"></uni-icons>
					<text style="font-size: small;">提交</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import models from '@/common/model.json';
	import psMethod from '@/common/psMethod';
	export default {
		data() {
			return {
				sConf: getApp().globalData,
			}
		},
		props: {
			depict: {
				type: Object,
				default: () => JSON.parse(JSON.stringify(models.jingle))
			}
		},
		methods: {
			EventSubmit() {
				console.log('EventSubmit');
				if (!this.depict.title || this.depict.title.length < 1) {
					uni.showToast({
						title: '商品标题能为空'
					})
					return
				}
				if (!this.depict.id) {
					console.log('new');
					this.loadPost()
				} else {
					console.log('put');
					this.loadPatch()
				}
			},
			async loadPost() {
				let host = this.sConf.host + '/admin/cargoText/';
				let token = this.sConf.auth.token || '';
				let data = psMethod.cutNull(this.depict)
				let res = await psMethod.authenticatorPostSync(host, token, data)
				if (res.rescode === 200) {
					uni.showToast({
						title: '提交成功'
					})
					this.$emit('reload')
				} else {
					uni.showModal({
						title: '失败',
						content: '创建失败',
						showCancel: false
					})
				}
			},
			async loadPatch() {
				let host = this.sConf.host + '/admin/cargoText/' + this.depict.id + '/';
				let token = this.sConf.auth.token || '';
				let data = psMethod.cutNull(this.depict)
				let res = await psMethod.authenticatorPatchSync(host, token, data)
				if (res.rescode === 200) {
					uni.showToast({
						title: '提交成功'
					})
					this.$emit('reload')
				} else {
					uni.showModal({
						title: '失败',
						content: '创建失败',
						showCancel: false
					})
				}
			}
		}
	}
</script>

<style lang="scss" scoped>

</style>